package com.qingfeng.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingfeng.client.bean.DataOnline;
import com.qingfeng.client.bean.SpringSessionBuyer;
import com.qingfeng.client.dao.DataOnlineMapper;
import com.qingfeng.client.dao.SpringSessionBuyerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/4/20 9:17 上午
 */
@Component
public class OnlineTask {

    @Autowired
    private SpringSessionBuyerMapper sessionBuyerMapper;

    @Autowired
    private DataOnlineMapper dataOnlineMapper;

    /**
     * 每隔30分钟，检查一次在线人数
     * 存储在线人数到服务器中
     */
    @Scheduled(fixedRate = 1800 * 1000)
    public void onlinePeople() {
        QueryWrapper<SpringSessionBuyer> queryWrapper = new QueryWrapper<>();
        int count = sessionBuyerMapper.selectCount(queryWrapper);
        System.out.println("任务执行时间：" + LocalDateTime.now());
        System.out.println("登录人数量：" + count);
        DataOnline dataOnline = new DataOnline();
        dataOnline.setCreateTime(new Date());
        dataOnline.setNum(count);
        dataOnlineMapper.insert(dataOnline);
    }

}
